<?php
/**
 * forumConnect Client
 *
 * @category   forumConnect
 * @package    forumConnect_Client
 * @copyright  Copyright (c) 2010 Benjamin Clot (benjamin.clot@gmail.com)
 */

error_reporting(E_ALL & ~E_NOTICE);

/**
 * forumConnect_Client
 *
 * @category   forumConnect
 * @package    forumConnect_Client
 * @copyright  Copyright (c) 2010 Benjamin Clot (benjamin.clot@gmail.com)
 */
class forumConnect_Client
{
	/**
	 * cURL handler
	 *
	 * @var	resource
	 */
	protected $_curl;
	
	/**
	 * Forum software
	 *
	 * @var	string
	 */
	protected $_software;
	
	/**
	 * Forum base URL (without trailing slash)
	 *
	 * @var	string
	 */
	protected $_serverbaseurl;
	
	/**
	 * forumConnect Server script name
	 *
	 * @var	string
	 */
	protected $_serverscriptname;
	
	/**
	 * Constructor
	 * 
	 * @param string
	 * @param string
	 * @param string
	 */
	public function __construct($software, $serverbaseurl, $serverscriptname)
	{
		$this->_serverbaseurl = $serverbaseurl;
		$this->_serverscriptname = $serverscriptname;
		$this->_software = $software;
		
		$this->_curl = curl_init($this->_serverbaseurl . '/' . $this->_serverscriptname);
		curl_setopt($this->_curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->_curl, CURLOPT_CONNECTTIMEOUT, 1);
		curl_setopt($this->_curl, CURLOPT_DNS_CACHE_TIMEOUT, 3600);
		curl_setopt($this->_curl, CURLOPT_TIMEOUT, 5);
		curl_setopt($this->_curl, CURLOPT_MAXCONNECTS, 100);
	}
	
	/**
	 * Destructor
	 */
	public function __destruct()
	{
		curl_close($this->_curl);
	}
	
	/**
	 * _execute
	 * 
	 * @return void
	 **/
	protected function _execute()
	{
		$data = curl_exec($this->_curl);
		
		echo $data;
	}
	
	/**
	 * getthread - get thread details
	 * 
	 * @param int
	 * @param boolean
	 * @param boolean
	 * @return void
	 */
	public function getthread($threadid, $getposts = false, $threaded = false)
	{
		curl_setopt($this->_curl, CURLOPT_POST, 1);
		curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 'do=getthread&threadid=' . (int)$threadid . '&getposts=' . (bool)$getposts . '&threaded=' . (bool)$threaded);
		
		$this->_execute();
	}
	
	/**
	 * getuser - get user details
	 * 
	 * @param int
	 * @return void
	 */
	public function getuser($userid)
	{
		curl_setopt($this->_curl, CURLOPT_POST, 1);
		curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 'do=getuser&userid=' . (int)$userid);
		
		$this->_execute();
	}
	
	/**
	 * isloggedin - check if user is logged in
	 * 
	 * @param string
	 * @return void
	 */
	public function isloggedin($cookieprefix = '')
	{
		switch($this->_software)
		{
			case 'vBulletin4':
				$sessionid = $_COOKIE[$cookieprefix . 'sessionhash'];
				$sessionuser = $_COOKIE[$cookieprefix . 'userid'] . ',' . $_COOKIE[$cookieprefix . 'password'];
				break;
			case 'xenForo':
				$sessionid = $_COOKIE[$cookieprefix . 'session'];
				$sessionuser = $_COOKIE[$cookieprefix . 'user'];
				break;
		}
		
		curl_setopt($this->_curl, CURLOPT_POST, 1);
		curl_setopt($this->_curl, CURLOPT_POSTFIELDS, 'do=isloggedin&ipaddress=' . $_SERVER['REMOTE_ADDR'] . '&useragent=' . $_SERVER['USER_AGENT'] . '&sessionid=' . $sessionid . '&sessionuser=' . $sessionuser);
		
		$this->_execute();
	}
}
